#region License and Copyright

/*
 * Copyright (C) 2008 Nathan Palmer
 * http://www.nathanpalmer.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author:
 *     Nathan Palmer <nathan1@nathanpalmer.com>
 */

#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Amazon.S3.com.amazonaws.s3;

namespace Amazon.S3
{
    public class S3Buckets : IEnumerable<S3Bucket>
    {
        private S3Helper helper;
        private List<S3Bucket> buckets;

        internal S3Buckets(S3Helper Helper)
        {
            helper = Helper;
        }

        private void LoadBuckets()
        {
            buckets = new List<S3Bucket>();
            SoapResponse<ListAllMyBucketsResult> soapResponse;
            if (helper.ListAllMyBuckets(out soapResponse))
            {
                foreach (ListAllMyBucketsEntry entry in soapResponse.State.Buckets)
                {
                    buckets.Add(new S3Bucket(helper, entry));
                }
            }
            else
            {
            }
        }

        public S3Bucket this[string BucketName]
        {
            get
            {
                if (buckets == null)
                    LoadBuckets();

                foreach (S3Bucket bucket in buckets)
                {
                    if (bucket.Name == BucketName)
                    {
                        return bucket;
                    }
                }
                return null;
            }

            //set
            //{
            //    foreach (Bucket bucket in buckets)
            //    {
            //        if (bucket.Name == BucketName)
            //        {
            //            bucket = value;
            //        }
            //    }
            //    return null;
            //}
        }

        public S3Bucket Add(string Name)
        {
            SoapResponse<CreateBucketResult> soapResponse;
            if (helper.CreateBucket(Name, out soapResponse))
            {
                LoadBuckets();
                return this[Name];
            }
            else
            {
                return null;
            }
        }

        public bool Delete(string Name, out string ErrorReason)
        {
            ErrorReason = "";
            try
            {

                if (buckets == null)
                    LoadBuckets();

                for (int i = 0; i < buckets.Count; i++)
                {
                    if (buckets[i].Name == Name)
                    {
                        try
                        {
                            SoapResponse<Status> soapResponse;
                            if (helper.DeleteBucket(Name, out soapResponse))
                            {
                                if (soapResponse.State.Code == 204)
                                {
                                    buckets.Remove(buckets[i]);
                                    return true;
                                }
                                else
                                {
                                    // Return code not 200
                                    ErrorReason = "(" + soapResponse.State.Code.ToString() + "): " + soapResponse.State.Description;
                                    return false;
                                }
                            }
                            else
                            {
                                ErrorReason = soapResponse.ErrorMessage;
                                return false;
                            }
                        }
                        catch (Exception ex)
                        {
                            // Exception
                            ErrorReason = ex.Message;
                            return false;
                        }
                    }
                }

                // Could not find the bucket
                return true;
            }
            catch (Exception ex)
            {
                ErrorReason = ex.Message;
                return false;
            }
        }

        #region IEnumerable<Bucket> Members

        public IEnumerator<S3Bucket> GetEnumerator()
        {
            if (buckets == null)
                LoadBuckets();

            return buckets.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
